using AdminAuthority.Attributes;

using Abc.Application;

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

using System.Threading.Tasks;

namespace Abc.Web.Mvc.Areas.HtAdmin.Controllers
{
    [AuthorityController(typeCode: "HtAdmin", controllerDescription: "用户")]
    public class AdminController : HtAdminBaseController
    {
        private readonly ILogger<UserController> _logger;
        private readonly IUserService _userService;

        public AdminController(ILogger<UserController> logger
        , IUserService userService)
        {
            _logger = logger;
            _userService = userService;
        }

        [HttpGet]
        public async Task<IActionResult> Index()
        {
            return View();
        }

        [HttpPost, HttpGet]
        public async Task<JsonResult> GetList(UserQuery searchquery)
        {
            var pagedlist = await _userService.GetAllByPageToLayui(searchquery);
            return Json(pagedlist);
        }

        public async Task<ActionResult> Info(int? id)
        {
            UserOut entity = null;
            if (id.HasValue)
            {
                entity = await _userService.Get(id.Value);
            }
            return View(entity ?? new UserOut());
        }

        [HttpPost]
        // [ValidateInput(false)]
        public async Task<JsonResult> Info(UserInput input)
        {
            return Json(await _userService.InsertOrUpdate(input));
        }

        [HttpPost]
        public async Task<ActionResult> Delete(int id)
        {
            return Json(await _userService.Delete(id));
        }

        [HttpPost]
        public async Task<JsonResult> SetState(int id, bool state, string type)
        {
            var resultdata = await _userService.SetState(id, state, type);
            return Json(resultdata);
        }
    }
}